package cn.zysuyuan.item.service.impl;

import cn.zysuyuan.common.utils.BeanMapUtils;
import cn.zysuyuan.item.client.UserClient;
import cn.zysuyuan.item.entity.Permission;
import cn.zysuyuan.item.entity.UserAndRole;
import cn.zysuyuan.item.mapper.UserMapper;
import cn.zysuyuan.item.service.UserService;
import cn.zysuyuan.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-18 15:29
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserClient userClient;

    @Override
    public int assignRoles(Map<String, Object> map) {

        // 先将之前分配的角色全部删除
        userMapper.delete((String) map.get("id"));
        String userId = (String)map.get("id");
        List<String> ids = (List<String>)map.get("ids");
        int inserts = 0;
        List<UserAndRole> rolelist = new ArrayList<>();
        if (ids != null && ids.size() > 0) {
            for (int i = 0; i < ids.size(); i++) {
                UserAndRole uRole = new UserAndRole();
                uRole.setUserId(userId);
                uRole.setRoleId(ids.get(i));
                rolelist.add(uRole);
            }
            inserts = userMapper.insert(rolelist);
        }
        return  inserts;
    }

    @Override
    public Map<String, Object> findAll() {
        List<Permission> list = userMapper.findAllPermission();
        Map<String,Object> map = new HashMap<>();
        List<String> menus = new ArrayList<>();
        List<String> points = new ArrayList<>();
        List<String> apis = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getType() == 1) {
                menus.add(list.get(i).getCode());
            } else if(list.get(i).getType() == 2) {
                points.add(list.get(i).getCode());
            } else if(list.get(i).getType() == 3) {
                apis.add(list.get(i).getCode());
            }
        }
        map.put("menus",menus);
        map.put("points",points);
        map.put("apis",apis);
        return map;
    }

    @Override
    public Map<String, Object> findRoles(String id) {
        List<Permission> list = userMapper.findUserPermission(id);
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).getCode().equals(list.get(i).getCode())) {
                    list.remove(j);
                }
            }
        }
        Map<String,Object> map = new HashMap<>();
        List<String> menus = new ArrayList<>();
        List<String> points = new ArrayList<>();
        List<String> apis = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getType() == 1) {
                menus.add(list.get(i).getCode());
            } else if(list.get(i).getType() == 2) {
                points.add(list.get(i).getCode());
            } else if(list.get(i).getType() == 3) {
                apis.add(list.get(i).getCode());
            }
        }
        map.put("menus",menus);
        map.put("points",points);
        map.put("apis",apis);
        return map;
    }

    @Override
    public Map<String, Object> getRoleList(String id) {
        User user = this.userClient.getRoleList(id);
        List<String> roleIds = this.userMapper.getRoleIds(id);
        Map<String,Object> map = BeanMapUtils.beanToMap(user);
        map.put("roleIds",roleIds);
        Map<String,Object> maps = new HashMap<>();
        maps.put("data",map);
        return  maps;
    }
}
